package com.sc.service.impl;

import com.mybatisflex.spring.service.impl.CacheableServiceImpl;
import com.sc.entity.Dictionaries;
import com.sc.mapper.DictionariesMapper;
import com.sc.service.DictionariesService;
import org.springframework.stereotype.Service;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;

import java.io.Serializable;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;

/**
 * 字典表 服务层实现。
 *
 * @author LiYe
 * @since 2024-09-25
 */
@Service
@CacheConfig(cacheNames = "dictionaries")
public class DictionariesServiceImpl extends CacheableServiceImpl<DictionariesMapper, Dictionaries> implements DictionariesService {

    @Override
    @CacheEvict(allEntries = true)
    public boolean remove(QueryWrapper query) {
        return super.remove(query);
    }

    @Override
    @CacheEvict(allEntries = true)
    public boolean removeById(Serializable id) {
        return super.removeById(id);
    }

    @Override
    @CacheEvict(allEntries = true)
    public boolean removeByIds(Collection<? extends Serializable> ids) {
        return super.removeByIds(ids);
    }

    @Override
    @CacheEvict(allEntries = true)
    public boolean update(Dictionaries entity, QueryWrapper query) {
        return super.update(entity, query);
    }

    @Override
    @CacheEvict(key = "#entity.id")
    public boolean updateById(Dictionaries entity, boolean ignoreNulls) {
        return super.updateById(entity, ignoreNulls);
    }

    @Override
    @CacheEvict(allEntries = true)
    public boolean updateBatch(Collection<Dictionaries> entities, int batchSize) {
        return super.updateBatch(entities, batchSize);
    }

    @Override
    @Cacheable(key = "#id")
    public Dictionaries getById(Serializable id) {
        return super.getById(id);
    }

    @Override
    @Cacheable(key = "#root.methodName + ':' + #query.toSQL()")
    public Dictionaries getOne(QueryWrapper query) {
        return super.getOne(query);
    }

    @Override
    @Cacheable(key = "#root.methodName + ':' + #query.toSQL()")
    public <R> R getOneAs(QueryWrapper query, Class<R> asType) {
        return super.getOneAs(query, asType);
    }

    @Override
    @Cacheable(key = "#root.methodName + ':' + #query.toSQL()")
    public Object getObj(QueryWrapper query) {
        return super.getObj(query);
    }

    @Override
    @Cacheable(key = "#root.methodName + ':' + #query.toSQL()")
    public <R> R getObjAs(QueryWrapper query, Class<R> asType) {
        return super.getObjAs(query, asType);
    }

    @Override
    @Cacheable(key = "#root.methodName + ':' + #query.toSQL()")
    public List<Object> objList(QueryWrapper query) {
        return super.objList(query);
    }

    @Override
    @Cacheable(key = "#root.methodName + ':' + #query.toSQL()")
    public <R> List<R> objListAs(QueryWrapper query, Class<R> asType) {
        return super.objListAs(query, asType);
    }

    @Override
    @Cacheable(key = "#root.methodName + ':' + #query.toSQL()")
    public List<Dictionaries> list(QueryWrapper query) {
        return super.list(query);
    }

    @Override
    @Cacheable(key = "#root.methodName + ':' + #query.toSQL()")
    public <R> List<R> listAs(QueryWrapper query, Class<R> asType) {
        return super.listAs(query, asType);
    }

    /**
     * @deprecated 无法通过注解进行缓存操作。
     */
    @Override
    @Deprecated
    public List<Dictionaries> listByIds(Collection<? extends Serializable> ids) {
        return super.listByIds(ids);
    }

    @Override
    @Cacheable(key = "#root.methodName + ':' + #query.toSQL()")
    public long count(QueryWrapper query) {
        return super.count(query);
    }

    @Override
    @Cacheable(key = "#root.methodName + ':' + #page.getPageSize() + ':' + #page.getPageNumber() + ':' + #query.toSQL()")
    public <R> Page<R> pageAs(Page<R> page, QueryWrapper query, Class<R> asType) {
        return super.pageAs(page, query, asType);
    }

    @Override
    @Cacheable(key = "#root.methodName")
    public List<HashMap> getSCMTree() {
        QueryWrapper queryWrapper = QueryWrapper.create().select("id", "name").eq("pid", 1);
        List<HashMap> schoolNode = listAs(queryWrapper, HashMap.class);
        for (HashMap school : schoolNode) {

            queryWrapper.clear();
            queryWrapper = QueryWrapper.create().select("id", "name").eq("pid", school.get("id"));
            List<HashMap> collageNode = listAs(queryWrapper, HashMap.class);
            for (HashMap collage : collageNode) {
                queryWrapper.clear();
                queryWrapper = QueryWrapper.create().select("id", "name").eq("pid", collage.get("id"));
                collage.put("children", listAs(queryWrapper, HashMap.class));
            }

            school.put("children", collageNode);
        }
        return schoolNode;
    }

    @Override
    @Cacheable(key = "#root.methodName + ':' + #id ")
    public String getNameById(Integer id) {
        QueryWrapper queryWrapper = QueryWrapper.create().select("name");
        // 从字典表查询学生年级
        queryWrapper.eq("id", id);
        return super.getOneAs(queryWrapper, String.class);
    }
}
